var Registration = {
	showRegistrationForm: function() {
		var xhr = new XMLHttpRequest();
		xhr.open("GET", "http://localhost:8080/Registration", true);
		xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xhr.send();
		xhr.onreadystatechange = function() {
			if (this.readyState === 4 && this.status === 200) {
				var regDiv = document.getElementById("reg-div");
				if (regDiv === null) {
					regDiv = document.createElement("div");
					regDiv.setAttribute("id", "reg-div");
					document.getElementById("middle-content").appendChild(regDiv);
					//TODO: Можливо треба очищати контент
				}
				regDiv.innerHTML = this.responseText;
			} else if (this.readyState === 4) {
				document.getElementById("status-panel").innerHTML = this.responseText;
//				alert();
//				alert('Не можливо встановити з’єднання з сервером авторизаціі.');
			}
		};
	},
	register: function() {
		var xhr = new XMLHttpRequest();
		xhr.open("POST", "http://localhost:8080/Registration", true);
		xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		var uLogin = document.getElementById("uLogin").value;
		var uPass = CryptoJS.MD5(document.getElementById("uPass").value);
		var passConfirm = CryptoJS.MD5(document.getElementById("pass-confirm").value);
		var uName = document.getElementById("uName").value;
		var uSurname = document.getElementById("uSurname").value;
		var uPhone = document.getElementById("uPhone").value;
		var uMail = document.getElementById("uMail").value;
		if (uLogin !== '' && uPass !== '' && passConfirm !== ''
				  && uPass === passConfirm && uName !== '' && uSurname !== ''
				  && uPhone !== '' && uMail !== '') {
//	CryptoJS.MD5(document.getElementById("uLogin").value

			var params = 'uLogin=' + uLogin
					  + '&uPass=' + uPass
					  + '&uName=' + uName
					  + '&uSurname=' + uSurname
					  + '&uPhone=' + uPhone
					  + '&uMail=' + uMail;

			xhr.send(params);
			xhr.onreadystatechange = function() {
				if (this.readyState === 4 && this.status === 200) {
					var regDiv = document.getElementById("reg-div");
					if (regDiv !== null) {
						regDiv.outerHTML = '';
					}
				} else if (this.readyState === 4) {
					document.getElementById("status-panel").innerHTML = this.responseText;
//				alert();
//				alert('Не можливо встановити з’єднання з сервером авторизаціі.');
				}
			};
		}
	},
	checkPass: function() {
		var pass = document.getElementById("pass").value;
		var passConfirm = document.getElementById("pass-confirm").value;
		var res = document.getElementById("pass-confirm-result");
		if (pass !== null && pass !== '' && passConfirm !== null && passConfirm !== '' && pass !== passConfirm) {
			res.innerHTML = 'Паролі не співпадають.';
		} else {
			res.innerHTML = '';
		}
	}
};

addElementClickListenerById("btnReg", Registration.showRegistrationForm);